<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/

	class Product_model extends CI_Model
	{
        protected $_table;

		public function __construct()
		{
			parent::__construct();

            $this->load->database();

            $this->_table = 'tbl_product';
		}

        /*Basic Method*/

        public function select($data = array())
        {
            if(empty($data))
            {
                $query = $this->db->get($this->_table);

                if($query->num_rows()>0)
                    return $query->result_object();
            }
            else
            {
                $query = $this->db->get_where($this->_table, $data);

                if($query->num_rows()>0)
                    return $query->row();
            }

            return FALSE;
        }

        public function edit($product_id, $data = array())
        {
            $data['slug'] = $this->create_unique_slug($data['product_title'],$this->_table,'slug','product_id', $product_id);

            if(isset($data['product_title']))
                $this->db->set('product_title', $data['product_title']);
            if(isset($data['product_name']))
                $this->db->set('product_name', $data['product_name']);
            if(isset($data['product_img']))
                $this->db->set('product_img', $data['product_img']);
            if(isset($data['product_thumb']))
                $this->db->set('product_thumb', $data['product_thumb']);
            if(isset($data['product_price']))
                $this->db->set('product_price', $data['product_price']);
            if(isset($data['product_price_sale']))
                $this->db->set('product_price_sale', $data['product_price_sale']);
            if(isset($data['product_quantity']))
                $this->db->set('product_quantity', $data['product_quantity']);
            if(isset($data['product_des']))
                $this->db->set('product_des', $data['product_des']);
            if(isset($data['product_status']))
                $this->db->set('product_status', $data['product_status']);
            if(isset($data['product_categories']))
                $this->db->set('product_categories', $data['product_categories']);
            if(isset($data['product_hightlight']))
                $this->db->set('product_hightlight', $data['product_hightlight']);
            if(isset($data['menu_slug']))
                $this->db->set('menu_slug', $data['menu_slug']);

            $this->db->set('slug', $data['slug']);

            $this->db->where('product_id', $product_id);

            if($this->db->update($this->_table))
                return TRUE;
            return FALSE;
        }

        public function delete($data = array())
        {
            $query = $this->db->delete($this->_table,$data);

            if($this->db->affected_rows()>0)
                return TRUE;
            return FALSE;
        }

        public function create($data = array())
        {
            $data['slug'] = $this->create_unique_slug($data['product_title'],$this->_table,'slug');

            $this->db->set('product_created', 'NOW()', FALSE);

            $query = $this->db->insert($this->_table,$data);

            if($this->db->affected_rows() > 0)
                return TRUE;
            return FALSE;
        }

        /*Magic Method*/
        protected function create_unique_slug($string, $table, $field='slug', $key=NULL, $value=NULL)
        {
            $t =& get_instance();
        	$slug = url_title($string);
        	$slug = strtolower($slug);
        	$i = 0;
        	$params = array ();
        	$params[$field] = $slug;


            if($key)
                $params["$key != "] = $value;

        	while ($t->db->where($params)->get($table)->num_rows())
        	{
        	    if (!preg_match ('/-{1}[0-9]+$/', $slug ))
        		    $slug .= '-' . ++$i;
        	    else
        		    $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );

        	    $params [$field] = $slug;
        	}

        	return $slug;
        }//End function create_unique_slug

        public function import_csv()
        {
            $config['upload_path'] = 'assets/uploads/csv/';
        	$config['allowed_types'] = 'csv|xml';
        	$config['max_size'] = '5000';
        	$config['remove_spaces'] = true;
        	$config['overwrite'] = true;
        	$this->load->library('upload', $config);
          	//$this->load->library('CSVReader');

        	if(!$this->upload->do_upload('csvfile'))
            {
        		$this->upload->display_errors();

                return false;
        	}

        	$csv = $this->upload->data();

        	$path = $csv['full_path'];

       	    //return $this->csvreader->parseFile($path);
            return $path;
        }

        public function csv2db()
        {
            unset($_POST['count']);
            unset($_POST['sub']);
         	unset($_POST['csvgo']);

            /*
         	foreach ($_POST as $line => $data){
         		if (isset($data['id'])){
         			$this->db->where('id',$data['id']);
         			unset($data['id']);
         			$this->db->update('products',$data);
         		}else{
         			$this->db->insert('products',$data);
         		}
         	}
            */

            $admin = $this->native_session->userdata('admin');

            $count = 1;

            foreach ($_POST as $datas)
            {
                foreach($datas as $data)
                {
                    if(isset($data['product_id']))
                    {
                        if(isset($data['product_title']))
                        {
                            $this->db->set('product_title', $data['product_title']);
                            $data['slug'] = $this->create_unique_slug($data['product_title'],$this->_table,'slug','product_id', $data['product_id']);
                        }

                        if(isset($data['product_name']))
                            $this->db->set('product_name', $data['product_name']);
                        if(isset($data['product_thumb']))
                        {
                            $data['product_thumb'] = 'assets/uploads/images/product/thumbs/'.$data['product_thumb'];
                            $this->db->set('product_thumb', $data['product_thumb']);
                        }
                        if(isset($data['product_price']))
                            $this->db->set('product_price', $data['product_price']);
                        if(isset($data['product_price_sale']))
                            $this->db->set('product_price_sale', $data['product_price_sale']);
                        if(isset($data['product_quantity']))
                            $this->db->set('product_quantity', $data['product_quantity']);
                        if(isset($data['product_des']))
                            $this->db->set('product_des', $data['product_des']);
                        if(isset($data['product_status']))
                            $this->db->set('product_status', $data['product_status']);
                        if(isset($data['product_categories']))
                            $this->db->set('product_categories', $data['product_categories']);
                        if(isset($data['product_hightlight']))
                            $this->db->set('product_hightlight', $data['product_hightlight']);
                        if(isset($data['menu_slug']))
                            $this->db->set('menu_slug', $data['menu_slug']);

                        $this->db->where('product_id', $data['product_id']);

                        $this->db->update($this->_table);
                    }
                    else
                    {
                        $data['menu_slug'] = $this->getMenuSlug($data['product_categories']);
                        $data['product_user_created'] = $admin['username'];
                        $data['slug'] = $this->create_unique_slug($data['product_title'],$this->_table,'slug');
                        $this->db->set('product_created', 'NOW()', FALSE);
                        //$this->db->set('product_name', $data['product_name']);
                        //echo $data['product_name'];
                        if(isset($data['product_thumb']))
                        {
                            $data['product_thumb'] = 'assets/uploads/images/product/thumbs/'.$data['product_thumb'];
                        }

             			$this->db->insert($this->_table,$data);
                    }
                }
         	}

        }//End function

        public function getMenuSlug($product_categories)
        {
            $strSQL = "SELECT slug FROM tbl_menu WHERE menu_id = ?";

            $query = $this->db->query($strSQL,$product_categories);

            $result = $query->row();

            return $result->slug;
        }
	}

/* End of file Product_model.php */
/* Location: ./models/backend/product_model.php */